// import { saveAs } from 'file-saver';
import { Homepage } from "../../../App";
import TDom from "../../../common/TDom";
import { mdRender } from "../../../common/Common";

class AlgorithmBase{
  constructor(id='AlgorithmBase')
  {
    this.id          = id;
    this.name        = '';
    this.description = '';
    // 图/表，训练和测试DOM对象
    let domElementList = {};
    domElementList.description  = document.getElementById('description');
    domElementList.collect      = document.getElementById('collect');
    domElementList.prepare      = document.getElementById('prepare');
    domElementList.analysis     = document.getElementById('analysis');
    domElementList.train        = document.getElementById('train');
    domElementList.test         = document.getElementById('test');
    domElementList.usage        = document.getElementById('usage');

    this.domElementList = domElementList;
    this.homepage       = `/${Homepage}`;
  }
  // 重置所有数据集和UI
  reset(){
    this.clear();

    // 重置算法使用的数据
    // 需要在继承类中实现
    this.clearData();
  }
  // 初始化训练集和测试集，和模型，必要时给定初值
  init(){ 
    this.reset();

    this.initDescription();

    // 初始化数据集
    // 需要在继承类中实现
    this.initDataset();
  }

  initDataset(){}

  initUIs(){
    this.initDescriptionUI();
    this.initCollectUI();
    this.initPrepareUI();
    this.initAnalysisUI();
    this.initTrainUI();
    this.initTestUI();
    this.initUsageUI();
  }

  initDescriptionUI(){
    this.descriptionDiv  = TDom.createDiv(this.domElementList.description);
  }
  // 从服务器获取描述文件*.md
  // 渲染到描述div
  initDescription(){
    TDom.getDataFromUrl(this.descriptionUrl, (txt)=>{
      mdRender(txt, this.descriptionDiv);
    })
  }
  initCollectUI(){
    this.collectDiv = TDom.createDiv(this.domElementList.collect);
  }
  initPrepareUI(){
    this.prepareDiv  = TDom.createDiv(this.domElementList.prepare);
  }
  initAnalysisUI(){
    this.analysisDiv  = TDom.createDiv(this.domElementList.analysis);
  }
  initTrainUI(){
    this.trainDiv  = TDom.createDiv(this.domElementList.train);
  }
  initTestUI(){
    this.testDiv  = TDom.createDiv(this.domElementList.test);
  }
  initUsageUI(){
    this.usageDiv  = TDom.createDiv(this.domElementList.usage);
  }
  // 清除DOM数据
  clear(){
    for (let key in this.domElementList) {
      if(this.domElementList[key]){
        this.domElementList[key].innerText = '';
      }
    }

    this.initUIs();
  }

  clearData(){

  }
}

export default AlgorithmBase;